{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "%reload_ext autoreload\n",
    "%autoreload 2\n",
    "%matplotlib inline\n",
    "import os\n",
    "os.environ[\"CUDA_DEVICE_ORDER\"]=\"PCI_BUS_ID\";\n",
    "os.environ[\"CUDA_VISIBLE_DEVICES\"]=\"0\";   "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Using TensorFlow backend.\n"
     ]
    }
   ],
   "source": [
    "import ktrain\n",
    "from ktrain import text"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Word Counts: 88582\n",
      "Nrows: 25000\n",
      "25000 train sequences\n",
      "Average train sequence length: 231\n",
      "x_train shape: (25000,400)\n",
      "y_train shape: (25000,2)\n",
      "25000 test sequences\n",
      "Average test sequence length: 224\n",
      "x_test shape: (25000,400)\n",
      "y_test shape: (25000,2)\n"
     ]
    }
   ],
   "source": [
    "# load training and validation data from a folder\n",
    "# download and unzip IMDb dataset:  https://ai.stanford.edu/~amaas/data/sentiment/aclImdb_v1.tar.gz\n",
    "DATADIR = 'data/aclImdb'\n",
    "(x_train, y_train), (x_test, y_test), preproc = text.texts_from_folder(DATADIR, \n",
    "                                                                         max_features=20000, maxlen=400, \n",
    "                                                                         ngram_range=1, \n",
    "                                                                         classes=['pos', 'neg'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "from tensorflow.keras.models import Sequential\n",
    "from tensorflow.keras.layers import Dense, Embedding, GlobalAveragePooling1D\n",
    "def get_model():\n",
    "    model = Sequential()\n",
    "    model.add(Embedding(20000+1, 50, input_length=400)) # add 1 for padding token\n",
    "    model.add(GlobalAveragePooling1D())\n",
    "    model.add(Dense(2, activation='softmax'))\n",
    "    model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])\n",
    "    return model\n",
    "model = get_model()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "learner = ktrain.get_learner(model, train_data=(x_train, y_train), val_data=(x_test, y_test))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "simulating training for different learning rates... this may take a few moments...\n",
      "Epoch 1/1024\n",
      "25000/25000 [==============================] - 5s 187us/step - loss: 0.6942 - acc: 0.4932\n",
      "Epoch 2/1024\n",
      "25000/25000 [==============================] - 4s 172us/step - loss: 0.5448 - acc: 0.7606\n",
      "Epoch 3/1024\n",
      "   32/25000 [..............................] - ETA: 4s - loss: 1.3311 - acc: 0.8750\n",
      "\n",
      "done.\n",
      "Please invoke the Learner.lr_plot() method to visually inspect the loss plot to help identify the maximal learning rate associated with falling loss.\n"
     ]
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAYYAAAEKCAYAAAAW8vJGAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4xLjEsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy8QZhcZAAAgAElEQVR4nO3deZhcZZn+8e9TW2/pdLqTzp6QBBKSIIQlbLIYVgEVVFCB0QFHhnFmHB1nXEBHZFRGZpSZcYFRQHEZFTHwY6JEwS2AsiUsSQghELKQlXTW7nSnu2t5fn+c053qTifpJn2qujr357rq4pxTp6ruLir11Hvec97X3B0REZEOsWIHEBGRgUWFQUREulBhEBGRLlQYRESkCxUGERHpQoVBRES6SBQ7QF+NGDHCJ02aVOwYIiIl5dlnn93q7vW92bfkCsOkSZNYtGhRsWOIiJQUM1vb2311KElERLpQYRARkS5UGEREpIvICoOZfd/MtpjZi/u538zs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S/DF+hsgYWbLCeb9fSqiCPcTDE/8EvC/wHPArl487mbgF2b2LLA1b/svgfd0dD4DHwdmh521L9HDzF7u/jLB9JXV3e8jKCofNrMlBF/Ynwi3/yPwT+H2o/aT+VjgGTN7Afgi8BV3bwc+QDAN7GLgtwS/9u8Argm3Tadr66jD3QTv03PhKazfZW/r7BzgoR4eI4OEht2Ww0rHMX8L5up9BjjD3TcXOMMngSZ3v7uX+1cCe9zdzexKgo7oyyINeeA8jwGXufuOYmWQaKmPQQ43vzKzYQSdyF8udFEI/Q/wvj7sfxJBZ7EBOwnOWCoKM6sn6G9RURjE1GIQEZEu1McgIiJdqDCIiEgXKgwiItKFCoOIiHShwiAiIl2oMIiISBf/HyqJdEDecgYQAAAAAElFTkSuQmCC\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "learner.lr_find()\n",
    "learner.lr_plot()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "\n",
      "begin training using triangular learning rate policy with max lr of 0.005...\n",
      "Train on 25000 samples, validate on 25000 samples\n",
      "Epoch 1/2\n",
      "25000/25000 [==============================] - 6s 241us/step - loss: 0.5540 - acc: 0.7531 - val_loss: 0.4021 - val_acc: 0.8615\n",
      "Epoch 2/2\n",
      "25000/25000 [==============================] - 6s 240us/step - loss: 0.3062 - acc: 0.8922 - val_loss: 0.2988 - val_acc: 0.8853\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<keras.callbacks.History at 0x7f7ad1b4a6d8>"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "learner.autofit(0.005, 2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "predictor = ktrain.get_predictor(learner.model, preproc)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "data = [ 'This movie was horrible! The plot was boring. Acting was okay, though.',\n",
    "         'The film really sucked. I want my money back.',\n",
    "        'What a beautiful romantic comedy. 10/10 would see again!']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['neg', 'neg', 'pos']"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "predictor.predict(data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "predictor.save('/tmp/mypred')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "predictor = ktrain.load_predictor('/tmp/mypred')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['neg']"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "predictor.predict(['The plot had lots of holes and did not make sense'])"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
